package com.example.votemanage.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.votemanage.entity.Auction;
import com.example.votemanage.mapper.AuctionMapper;
import com.example.votemanage.service.AuctionService;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;

@Service
public class AuctionServiceImpl  extends ServiceImpl<AuctionMapper, Auction> implements AuctionService {
    public <E extends IPage<Auction>> E pageWithConditions(E page, Map<String, Object> conditions) {
        QueryWrapper<Auction> queryWrapper = new QueryWrapper<>();
        Iterator<Map.Entry<String, Object>> iterator = conditions.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            // 如果是分页参数，则从conditions中移除
            if (key.equals("pageIndex") || key.equals("pageSize")) {
                iterator.remove();
            } else if (entry.getValue() != null) {
                // 否则添加到QueryWrapper中
                queryWrapper.like(key, entry.getValue());
            }
        }
        queryWrapper.orderByDesc("auctionId");
        return this.page(page, queryWrapper);
    }
}
